<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="signupForm">
                        <input id="id" name="id" th:value="${timu.id}" type="hidden">
                        <input id="tixuhao" name="id" th:value="${timu.tixuhao}" type="hidden">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">类型：</label>
                            <div class="col-sm-5">
                                <select data-placeholder="选择用户" id="titimu" name="titimu"
                                        class="form-control chosen-select" onchange="hide(this.value)">
                                    <span th:if="${timu.titimu} eq '1'">
                                        <option value="1">判断题</option>
                                        <option value="2">单选题</option>
                                        <option value="3">多选题</option>
                                    </span>
                                    <span th:if="${timu.titimu} eq '2'">
                                        <option value="2">单选题</option>
                                        <option value="1">判断题</option>
                                        <option value="3">多选题</option>
                                    </span>
                                    <span th:if="${timu.titimu} eq '3'">
                                        <option value="3">多选题</option>
                                        <option value="1">判断题</option>
                                        <option value="2">单选题</option>

                                    </span>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">题目：</label>
                            <div class="col-sm-5">
                                <input id="titigan" name="titigan" class="form-control" type="text" required=""
                                       aria-required="true" th:value="${timu.titigan}">
                            </div>
                        </div>
                        <div class="form-group" id="addxuanxiang" style="display: none;">
                            <div class="col-sm-5 col-sm-offset-3">
                                <button type="button" class="btn btn-primary" style="background-color: darkred"
                                        onclick="addxx()">增加选项
                                </button>
                            </div>
                        </div>
                        <span id="xuangxiangs">
                            <div class="form-group" style="background-color:  #00B83F" th:each="xuanxiang:${list}">
                            <label class="col-sm-3 control-label">选项：</label>
                            <div class="col-sm-5">
                                <input id="xuanxiang" name="xuanxiang" class="form-control" type="text"
                                       th:value="${xuanxiang.xuanxiang}"
                                       required=""
                                       aria-required="true">
                            </div>
                        </div>
                        </span>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">正确答案：</label>
                            <div class="col-sm-5">
                                <input id="tizhengque" name="tizhengque" class="form-control" type="text"
                                       th:value="${timu.tizhengque}" required=""
                                       aria-required="true">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">题目解析：</label>
                            <div class="col-sm-5">
                                <textarea id="tijiexi" name="tijiexi" class="form-control" required=""
                                          aria-required="true" th:text="${timu.tijiexi}"></textarea>
                            </div>
                        </div>
                        <input id="zhishidianid" name="zhishidianid" class="form-control" type="hidden"
                               th:value="${zhishidianid}">
                        <div class="form-group">
                            <div class="col-sm-5 col-sm-offset-3">
                                <button type="submit" class="btn btn-primary">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="count" value="2">
<div th:include="include::footer"></div>
<script>
    $().ready(function () {
        validateRule();
    });

    $.validator.setDefaults({
        submitHandler: function () {
            update();
        }
    });

    function addxx() {
        var val = $('#count').val();
        if (val == 6) {
            layer.msg("选项最多为6个")
        } else {
            var txt = "<div class=\"form-group\" style=\"background-color: #00B83F\">\n" +
                "<label class=\"col-sm-3 control-label\">选项：</label>\n" +
                "<div class=\"col-sm-5\">\n" +
                "<input id=\"xuanxiang\" name=\"xuanxiang\" class=\"form-control\" type=\"text\" value=\"\" required=\"\"\n" +
                "aria-required=\"true\">\n" +
                "</div>\n" +
                "</div>";
            $('#xuangxiangs').append(txt)
            $('#count').val($('#count').val() - 0 + 1)
        }
    }

    function hide(val) {
        if (val != 1) {
            $("#addxuanxiang").css("display", "block");
        } else {
            $("#addxuanxiang").css("display", "none");
        }
    }

    function update() {
        var flag = true;
        var xuangxiang = new Array();
        $("input[name='xuanxiang']").each(function (i) {
            if (this.value == '') {
                flag = false;
                return;
            }
            xuangxiang[i] = this.value;
        });
        if (flag == false) {
            layer.msg('选项内容不能为空');
            return;
        }
        var tixuhao = $('#tixuhao').val();
        var re = new RegExp("^[0-9]*$");
        if (re.test(tixuhao)) {
            $.ajax({
                cache: true,
                type: "POST",
                url: "/contract/timu/update",
                data: $('#signupForm').serialize(),// 你的formid
                async: false,
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.code == 0) {
                        parent.layer.msg("操作成功");
                        parent.reLoad();
                        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                        parent.layer.close(index);

                    } else {
                        parent.layer.alert(data.msg)
                    }

                }
            });
        } else {
            //提示层
            layer.msg('序号只能是数字');
        }

    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#signupForm").validate({
            rules: {
                name: {
                    required: true
                }
            },
            messages: {
                name: {
                    required: icon + "请输入名字"
                }
            }
        })
    }
</script>
</body>
</html>
